/**
 * represent a doctor type user
 *
 */

package source;
import java.util.List;
import java.util.Map;
import activejdbc.Model;

public class Doctor extends Model {
	
	public Doctor() {
		super();
	}
	
	public Doctor(Map<String, String> values) {
		this.fromMap(values);
	}
	
	/**
	 * add a patient, set necessary fields, and return it
	 * 
	 * @param values	key-map, any values passed in, match them and fill in
	 * @return 			patient that is created
	 */
	public void addPatient(Map<String, String> values) {
		Patient p = new Patient(values);
		p.saveIt();
	}
	
	/**
	 * find a patient by username, then delete from database
	 * 
	 * @param values 	key-map, any values passed in, match them and find
	 */
	public void deletePatient(Map<String, String> values) {
		Patient aPatient = (Patient)Patient.findFirst("username = ?", values.get("username"));
		aPatient.delete();
	}
	
	/**
	 * find patient(s) by the parameter(s) and return the list of patient(s)
	 * 
	 * @param values 	key-map, any values passed in, match them to find
	 * @return 			list of Patient that are matched
	 */
	public Patient lookupPatient(Map<String, String> values) {
		if (values.containsKey("id"))
			return (Patient)Patient.findById(values.get("id"));
		else if (values.containsKey("username"))
			return (Patient)Patient.findById(values.get("username"));
		else
			return (Patient)Patient.findById(values.get("fullname"));
		//return (Patient)Patient.findFirst("username = ?", values.get("username"));
	}
	
	/**
	 * updates patient parameters
	 * 
	 * @param values 	key-map, any values passed in, match them to find
	 */
	public void updatePatient(Patient p, Map<String, String> values) {
		p.fromMap(values);
		p.saveIt();
	}
	
	/**
	 * create a treatment record with values passed in
	 * 
	 * @param values	key-map, any values passed in, match them and fill in
	 * @return			TreatmentRecord that is created
	 */
	
	public List<Patient> getAllPatients() {
		return Patient.findAll();
	}
	
	
	// create a doctor's order with patient name and other parameters
	/**
	 * doctor can create doctor's orders on patient's treatment
	 * @param	values	map that contains fields and values
	 * 
	 */
	public void createDoctorsOrders(Map<String, String> values) {
		assert(values.containsKey("treatment_record_id"));
		DoctorsOrders ord = new DoctorsOrders(values);
		ord.set("doctor_id", this.get("id"));
		ord.saveIt();
	}

	// look up by doctors orders or treatment record?
	/**
	 * look up doctors orders
	 * @param	values	map that contains fields and values
	 * @return	order object
	 */
	public DoctorsOrders lookupDoctorsOrders(Map<String, String> values) {
		return (DoctorsOrders) DoctorsOrders.findFirst("id = ?", values.get("id"));
	}
	
	/**
	 * look up all doctors orders
	 * @param	values	map that contains fields and values
	 * @return	order object
	 */
	public List<DoctorsOrders> getAllDoctorsOrders() {
		return this.getAll(DoctorsOrders.class);
	}
	
	/**
	 * look up doctors orders and update 
	 *
	 * @param values 	key-map, any values passed in, match and update
	 */
	public void updateDoctorsOrders(Map<String, String> values) {
		DoctorsOrders ord = (DoctorsOrders)DoctorsOrders.findById(values.get("id"));
		ord.fromMap(values);
		ord.saveIt();
	}
	
	/**
	 * find doctors orders by id and delete it from the database
	 *
	 * @param values 	find the matching DoctorsOrders with passed in values
	 */
	public void deleteDoctorsOrders(Map<String, String> values) {
		DoctorsOrders ord = (DoctorsOrders) DoctorsOrders.findById(values.get("id"));
		ord.delete();
	}
	
	// to be filled
	public void generateReport() {
		
	}
}
